package loveATM.atm.Panel;

import loveATM.User;
import loveATM.atm.ATM;
import loveATM.atm.FileUtil;
import loveATM.atm.Font.MyFont;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 转账界面
 */
public class TransferPanel extends JPanel implements ActionListener {
    private ATM atm;
    MyFont myFont = new MyFont();
    private JLabel accountLabel = new JLabel("请输入账号:");
    private JTextField accountTextField = new JTextField();
    private JLabel inputLabel = new JLabel("请输入转入金额:");
    private JTextField inputTextField = new JTextField();

    private JButton sureBtn = new JButton(new ImageIcon("images/buttonSure.jpg"));
    private JButton offBtn = new JButton(new ImageIcon("images/buttonOff.jpg"));
    public TransferPanel(ATM atm){
        this.atm = atm;
        this.setLayout(null);
        this.setOpaque(false);


        accountLabel.setBounds(205,200,200,50);
        accountLabel.setFont(myFont.font);
        accountTextField.setBounds(400,200,250,50);
        accountTextField.setFont(myFont.font);
        this.add(accountLabel);
        this.add(accountTextField);

        inputLabel.setBounds(205,250,200,50);
        inputLabel.setFont(myFont.font);
        inputTextField.setBounds(400,250,250,50);
        inputTextField.setFont(myFont.font);
        this.add(inputLabel);
        this.add(inputTextField);
        sureBtn.setBounds(1024-190,300,140,50);
        sureBtn.addActionListener(this);
        this.add(sureBtn);

        offBtn.setBounds(1024-190,450,140,50);
        offBtn.addActionListener(this);
        this.add(offBtn);

    }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == offBtn){
                accountTextField.setText("");
                inputTextField.setText("");
                atm.showPanel("menu");
                return;
            }
            ArrayList<User> users = atm.getUsers();
            int  index = atm.index;
            if (inputTextField.getText().equals("")){
                JOptionPane.showMessageDialog(this,"请输入金额");
                return;
            }
            double input = 0;
            try {
                input = Double.parseDouble(inputTextField.getText());
            }catch (NumberFormatException exception){
                JOptionPane.showMessageDialog(this,"请输入数字");
                inputTextField.setText("");
                return;
            }

            if (e.getSource() == sureBtn){

                if (accountTextField.getText().equals(users.get(index).getAccount())){
                    JOptionPane.showMessageDialog(this,"不能转账给自己");
                    accountTextField.setText("");

                    return;
                }
                boolean isExist = false;
                int otherIndex = 0;
                for(int i = 0;i < users.size();i++){
                    if(users.get(i).getAccount().equals(accountTextField.getText())){
                        isExist = true;
                        otherIndex = i;
                        break;
                    }
                }
                if(!isExist){
                    JOptionPane.showMessageDialog(this,"输入的对方账号错误");
                    return;
                }
                if (users.get(index).getBlance() > input){
                            users.get(index).setBlance(users.get(index).getBlance() - input);
                            users.get(otherIndex).setBlance(users.get(otherIndex).getBlance() + input);
                            FileUtil.writeObject(users,atm.FILE_PATH);
                    JOptionPane.showMessageDialog(this,"转账成功");
                            accountTextField.setText("");
                            inputTextField.setText("");
                            return;
                }else {
                            JOptionPane.showMessageDialog(this,"金额不足");
                            accountTextField.setText("");
                            inputTextField.setText("");
                            return;

                }

            }


        }


}
